import type { Module } from 'vuex'
import lkfRequest from '@/services'
import { IRootState } from '../types'
import { ISong } from './types'
/* 歌曲store */
const song: Module<ISong, IRootState> = {
  namespaced: true,
  state() {
    return {
      songDetail: {},
      songInfo: {},
      lyric: '',
      playList: []
    }
  },
  getters: {},
  mutations: {
    getSongDetailGetter(state, res) {
      state.songDetail = res
    },
    getSongInfoGetter(state, res) {
      state.songInfo = res[0]
      state.playList.unshift(res[0])
    },
    getSongLyricGetter(state, res) {
      state.lyric = res
    }
  },
  actions: {
    /* 请求数据 */
    async getSongDetailAction({ commit }, payload) {
      const canPlay = await lkfRequest.get({
        url: `/check/music?id=${payload.id}`
      })

      if (canPlay.success) {
        const res = await lkfRequest.get({
          url: `/song/url?id=${payload.id}`
        })
        commit('getSongDetailGetter', res.data)
      } else {
        console.log('无版权无法播放')
      }
    },
    async getSongInfoAction({ commit }, payload) {
      const res = await lkfRequest.get({
        url: `/song/detail?ids=${payload.id}`
      })
      commit('getSongInfoGetter', res.songs)
    },
    async getSongLyricAction({ commit }, payload) {
      const res = await lkfRequest.get({
        url: `/lyric?id=${payload.id}`
      })
      commit('getSongLyricGetter', res.lrc.lyric)
    }
  }
}
export default song
